* ======================================
* ----------- Program: Match -----------
* ======================================

	preserve

	* args:
	* 1: source for voters sample,
	* 2: source for politicians sample
	* 3: network
	* 4: (netmatch/idmatch) matches on network id or unique person id
	* 5: (dist/nodist): compute or bypass distance computation 

	
* Politicians
* ===========

	use `2', clear

	if "`4'" == "netmatch"{
		rename Polit_id idPers
		joinby idPers using $dta\Network`3'.dta
		rename idPers `3'Pol_id
		rename Polit_bsu `3'Pol_bsu
		rename Polit_knr `3'Pol_knr
		rename Polit_RankPct `3'Pol_pRank
		rename Polit_Rank `3'Pol_rank
		rename Polit_Party `3'Pol_party			
		keep `3'net year `3'Pol_*
	}
	
	if "`4'" == "idmatch"{
		rename Polit_id `3'Pol_id
		rename Polit_bsu `3'Pol_bsu
		rename Polit_knr `3'Pol_knr
		rename Polit_RankPct `3'Pol_pRank
		rename Polit_Rank `3'Pol_rank
		rename Polit_Party `3'Pol_party		
		keep year `3'Pol_*
	}
	

	tempfile Polit_`3'
	save `Polit_`3'', replace
	
	
* Voters
* ======

	use `1', clear
	
	rename Voter_id idPers
	joinby idPers using $dta\Network`3'.dta
	rename idPers Voter_id
	
	if "`4'" == "netmatch"{
		keep Voter_id year Voter_knr Voter_bsu `3'net
	}
	
	if "`4'" == "idmatch"{
		rename id`3' `3'Pol_id // facilitate match by id
		keep Voter_id year Voter_knr Voter_bsu `3'Pol_id
	}
	
	
* Match
* =====

	if "`4'" == "netmatch"{
		joinby `3'net year using `Polit_`3''
	}
	if "`4'" == "idmatch"{
		joinby `3'Pol_id year using `Polit_`3''
	}
	
	* Drop politicians outside if voter has connection(s) inside
	gen same = 1*(Voter_knr == `3'Pol_knr) 			// Same District
	bysort Voter_id year: egen same_max = max(same) // 1 if n >= 1 in same D.
	bysort Voter_id year: keep if same == same_max
	drop same*
		
		
* Reduce dimensions
* =================

	* Using min(Distance)
	if "`5'" == "dist"{
		rename Voter_bsu bsu_i
		rename `3'Pol_bsu bsu_j
		merge m:1 bsu_i bsu_j using "$dta\ImportDistance.dta", ///
			nogen keepusing(dist_km) keep(master matched) 
		replace dist_km = 0 if bsu_i == bsu_j 	// Distance = 0 in same bsu
		rename dist_km `3'Pol_dist
		rename bsu_i Voter_bsu
		rename bsu_j `3'Pol_bsu

		* Keep closest (anyone with missing distance last)
		bysort Voter_id year (`3'Pol_dist `3'Pol_id): keep if _n == 1	
		keep Voter_id year `3'Pol_dist `3'Pol_id `3'Pol_pRank `3'Pol_knr ///
			`3'Pol_bsu `3'Pol_rank `3'Pol_party
	}
	
	
	* Using max(Rank)
	if "`5'" == "rank"{
		* Keep highest rank
		bysort Voter_id year (`3'Pol_pRank): keep if _n == 1
		keep Voter_id year `3'Pol_id `3'Pol_knr `3'Pol_rank `3'Pol_party
	}
	
	
	tempfile match`3'
	save `match`3''
		
	restore
	
	
* Match to default dataset
* ========================

	* Voters and politicians
	merge 1:1 Voter_id year using `match`3'', nogen ///
		keep(master matched)
	
	* Generate estimation variables
	gen `3'Any = 1*(`3'Pol_id != "") 						// Any variable
	gen `3'Same = 1*(`3'Any == 1 & Voter_knr == `3'Pol_knr) // Same variable


